<?php

namespace Admin\Controller;


class ProductController extends CommonController
{

    public function __construct()
    {
        parent::__construct();
        $categories = $this->all_categories();

        $this->assign('categories', $categories);
    }

    /**
     * 商城首页---查出所有分类
     */
    public function all_categories()
    {

        $categories = $this->category->where("parent_id = 0")->select();
        foreach ($categories as &$category) {
            $category['children'] = $this->category->where("parent_id = $category[id]")->select();
        }
        return $categories;
    }

    /**
     * 商城首页
     */
    public function index()
    {
        //模糊查询
        $name = I('get.name');
        $category_id = I('get.category_id');

        if (isset($name)) {
            $map['name'] = array('like', '%' . $name . '%');
        }
        if (isset($category_id)) {
            $map['category_id'] = array('like', '%' . $category_id . '%');
        }
        //这个不能模糊查询，只能在分类里查看商品
//        if(!empty($category_id)){
//            $map['category_id'] = $category_id;
//        }

        $count = $this->product->where('status=1')->count();// 查询满足要求的总记录数
        $Page = new \Common\Util\Page($count, 6);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $pages = $Page->show();// 分页显示输出

        $products = $this->product->where($map)->where("status = 1")->order("sort_order desc")->limit($Page->firstRow . ',' . $Page->listRows)->select();
        foreach ($products as &$product) {
            $product['category'] = $this->category->where("id = '$product[category_id]'")->find();
        }

        $datas = array('is_top', 'is_new', 'is_hot', 'is_recommend', 'is_onsale');
        $this->assign(compact('products', 'pages', 'count', 'datas'));
        $this->display();
    }

    /**
     * 商城首页---新增
     */
    public function add()
    {
        if (IS_POST) {
            $Product = D("Product"); // 实例化User对象
            if (!$Product->create()) { // 创建数据对象
                // 如果创建失败 表示验证没有通过 输出错误提示信息
                $this->error($Product->getError());
            } else {
                // 验证通过 写入新增数据
                $Product->create();
                $id = $Product->add();
                if ($id) {
                    $this->success('新增成功', U('index'));
                }

                //将多图插入gallery数据库
                $galleries = I('post.gallery');
                foreach ($galleries as $value) {
                    $gallery['product_id'] = $id;
                    $gallery['img'] = $value;
                }
                $this->gallery->where("product_id = '$id'")->add($gallery);
            }
        } else {
            $this->display();
        }
    }

    /**
     * 商城首页---编辑
     */
    public function edit()
    {

        $id = I('get.id');
        if (IS_POST) {
            $Product = D("Product"); // 实例化User对象
            if (!$Product->create()) { // 创建数据对象
                // 如果创建失败 表示验证没有通过 输出错误提示信息
                $this->error($Product->getError());
            } else {
                // 验证通过 写入新增数据
                $Product->create();

                //把商品相册插入相册表,先判断相册表是否有该商品的相册，没有就插入新的图片
                $galleries = I('post.gallery');
                $result = $this->gallery->where("product_id = '$id'")->select();
                if ($result) {
                    //如果图片表已经有对应的图片，先把图片删掉，再插入新的图片
                    $this->gallery->where("product_id = '$id'")->delete();
                    foreach ($galleries as $v) {
                        $gallery['img'] = $v;
                        $gallery['product_id'] = $id;
                        $this->gallery->add($gallery);
                    }
                } else {
                    foreach ($galleries as $v) {
                        $gallery['img'] = $v;
                        $gallery['product_id'] = $id;
                        $this->gallery->add($gallery);
                    }
                }

                $Product->where("id = '$id'")->save();
                $this->success('编辑成功', U('index'));
            }
        } else {
            $product = $this->product->where("id = '$id'")->find();
            //查出所有相册
            $galleries = $this->gallery->where("product_id = '$id'")->select();

            $this->assign('product', $product);
            $this->assign('galleries', $galleries);
            $this->display();
        }

    }

    /**
     * 商城首页--单个回收
     */
    public function ajax_recycle()
    {
        if (IS_AJAX) {
            $id = I('post.id');
            $this->product->where("id = '$id'")->setField('status', 0);
        }
    }

    /**
     * 商城首页--多个回收
     */
    public function ajax_recycle_all()
    {
        if (IS_AJAX) {
            $ids = I('post.ids');
            foreach ($ids as $id) {
                $this->product->where("id = '$id'")->setField('status', 0);
            }
        }
    }

    /**
     * 商城首页--排序
     */
    public function ajax_sort_order()
    {
        if (IS_AJAX) {
            $id = I('post.id');
            $sort_order = I('post.sort_order');
            $this->product->where("id = '$id'")->setField('sort_order', $sort_order);
        }
    }

    /**
     * 商城首页--库存管理
     */
    public function ajax_sort_stock()
    {
        if (IS_AJAX) {
            $id = I('post.id');
            $stock = I('post.stock');
            $this->product->where("id = '$id'")->setField('stock', $stock);
        }
    }

    /**
     * 商城首页---ajax改变属性
     */
    public function change_attr()
    {
        if (IS_AJAX) {
            $id = I('post.id');
            $attr = I('post.attr');
            $product = $this->product->find($id);
            $this->product->where("id = '$id'")->setField($attr, !$product[$attr]);
        }

    }

}
